package cn.caskm.souvenirshopsystem.controller.api;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.po.GoodsComment;
import cn.caskm.souvenirshopsystem.domain.query.GoodsCommentQuery;
import cn.caskm.souvenirshopsystem.domain.vo.GoodsCommentVO;
import cn.caskm.souvenirshopsystem.service.GoodsCommentService;
import cn.caskm.souvenirshopsystem.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {

    private final GoodsCommentService commentService;

    @PostMapping("/addComment")
    public Result<Object> addComment(@RequestBody GoodsComment comment) {
        Long userId = UserContext.getUser();
        comment.setUserId(userId.intValue());
        comment.setCommentTime(LocalDateTime.now());
        commentService.save(comment);
        return Result.success();
    }

    @GetMapping("/listGoodsComment")
    public Result<PageDTO<GoodsCommentVO>> listGoodsComment(GoodsCommentQuery query) {
        return Result.success(commentService.listGoodsComment(query));
    }

}
